import operator
from dataclasses import dataclass, field


class Student1(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __repr__(self):
        return f"Student1(name={self.name}, age={self.age})"


#####################   约等于      ###############################

@dataclass
class Student2:
    name: str
    age: int


@dataclass(frozen=True)  # 初始化实力参数后，不能更改
class Student3:
    name: str
    age: int


s1 = Student2("张三", 18)
print(s1)
s2 = Student2("李四", 19)
print(s2)


@dataclass(order=True)  # 可以排序，默认按照属性顺序进行排序
class Student4:
    sort_index: int = field(init=False, repr=False)
    name: str
    age: int
    independent: bool = field(default=False, init=False, repr=True)

    def __post_init__(self):
        self.independent = self.age > 20
        self.sort_index = self.age


s3 = Student4("王五", 15)
s4 = Student4("赵六", 21)
s5 = Student4("田七", 22)
s = [s3, s4, s5]
print(s)
# 排序2
s.sort(key=operator.attrgetter("age"))
print(s)
